<template>
  <div class="login-container">
    <h1>登录页面</h1>
    <form @submit.prevent="handleSubmit">
      <div class="form-group">
        <label for="username">账号:</label>
        <input v-model="loginUser.username" id="username" type="text" required placeholder="请输入账号" />
      </div>
      <div class="form-group">
        <label for="password">密码:</label>
        <input v-model="loginUser.password" id="password" type="password" required placeholder="请输入密码" />
      </div>
      <button type="submit">登录</button>
    </form>
    <p v-if="errorMessage" class="error-message">{{ errorMessage }}</p>
  </div>
</template>

<script setup>
import { reactive, ref } from 'vue';
import { loginApi } from '../api/login.js';
import { useRouter } from 'vue-router'; 
const router = useRouter();
const loginUser = reactive({
  username: '',
  password: ''
});

const errorMessage = ref('');

// 提交表单
const handleSubmit = async () => {
  if (!loginUser.username) {
    errorMessage.value = '请输入账号';
    return;
  }
  if (!loginUser.password) {
    errorMessage.value = '请输入密码';
    return;
  }
  console.log(loginUser);

  try {
    const resp = await loginApi(loginUser.username, loginUser.password);
    console.log("resp", resp)
    if (resp.data.code === 200) {
      console.log('Response:', resp);
      localStorage.setItem('token', resp.data.token); // 存储 token
      localStorage.setItem('customer', resp.data.customer);
      localStorage.setItem('userId', resp.data.userId);
      errorMessage.value = ''; // 清除错误消息

      // 登录成功后根据设备类型跳转
      const isMobile = /iPhone|iPad|iPod|Android/i.test(navigator.userAgent);
      console.log('isMobile:', isMobile);
      console.log('userId:', resp.data.userId);
      //去本地存储拿userId的值
      const userIdValue = localStorage.getItem('userId');
      if (isMobile) {
        router.push({ name: 'mobile' }); // 移动设备重定向到 MobileLayout.vue
      } else {
        if (userIdValue === '1001') {
          router.push({ name: 'app3' }); // 桌面设备重定向到 App3.vue
          
        }else{
          router.push({ name: 'mobile' }); // 重定向到 
         
        }
      }
      
    } else {
      errorMessage.value = resp.data.msg;
    }
  } catch (error) {
    errorMessage.value = error.message || '登录失败';
  }
};
</script>

<style scoped>
.login-container {
  background-image:url('../assets/imgs/login_background.jpg');
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 95vh;
  padding: 20px;
  width:100%;
  background-size:cover;
  background-position: center;
}

.form-group {
  margin-bottom: 10px;
}

label {
  display: block;
  margin-bottom: 5px;
}

input[type="text"],
input[type="password"] {
  width: 100%;
  padding: 8px;
  border: 1px solid #ccc;
  border-radius: 4px;
}

button {
  padding: 10px 20px;
  background-color: #007bff;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

button:hover {
  background-color: #0056b3;
}

.error-message {
  color: red;
  margin-top: 10px;
}
</style>